using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.Bll
{
    public abstract class BaseComparison<T> : System.Collections.Generic.Comparer<T>
    {
        protected BaseComparison()
        {
        }

        internal void Set(string field, bool ascending)
        {
            this.field = field;
            this.ascending = ascending;
        }

        private string field;
        public string Field
        {
            get { return field; }
        }


        private bool ascending;
        public bool Ascending
        {
            get { return ascending; }
        }


        protected int GetReturnValue(int x, int y)
        {
            return x.CompareTo(y) * (ascending ? 1 : -1);
        }

        protected int GetReturnValue(decimal x, decimal y)
        {
            return x.CompareTo(y) * (ascending ? 1 : -1);
        }

        protected int GetReturnValue(byte x, byte y)
        {
            return x.CompareTo(y) * (ascending ? 1 : -1);
        }

        protected int GetReturnValue(DateTime x, DateTime y)
        {
            return x.CompareTo(y) * (ascending ? 1 : -1);
        }

        protected int GetReturnValue(string x, string y)
        {
            if (x != null && y != null)
            {
                return x.ToLower().CompareTo(y.ToLower()) * (ascending ? 1 : -1);
            }
            else if (x != null)
            {
                return x.CompareTo(y) * (ascending ? 1 : -1);
            }
            else if (y != null)
            {
                return y.CompareTo(x) * (ascending ? -1 : 1);
            }
            else
            {
                return 0;
            }
        }

        protected int GetReturnValue(System.Net.IPAddress x, System.Net.IPAddress y)
        {
            if (x != null && y != null)
            {
                return x.AddressFamily.CompareTo(y.AddressFamily) * (ascending ? 1 : -1);
            }
            else if(x != null)
            {
                return x.AddressFamily.CompareTo(null) * (ascending ? 1 : -1);
            }
            else if (y != null)
            {
                return y.AddressFamily.CompareTo(null) * (ascending ? -1 : 1);
            }
            else
            {
                return 0;
            }
        }

        protected int GetReturnValue(bool x, bool y)
        {
            return x.CompareTo(y) * (ascending ? 1 : -1);
        }

        private int GetEqual()
        {
            return 0;
        }

        private int GetGreaterThan()
        {
            return ascending ? 1 : -1;
        }

        private int GetLessThan()
        {
            return GetGreaterThan() * -1;
        }
    }
}
